package com.boshanam.view.forms.user;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.ValidatorForm;

import com.boshanam.model.vos.domain.user.UserSettingsVO;
import com.boshanam.model.vos.domain.user.UserVO;

/**
 * @author Sivakumar Y, 
 * UserRegForm.java Jan 3, 2010 1:38:22 AM
 * www.ibigbang.net
 * shiva.forums@gmail.com
 */
public class UserRegForm extends ValidatorForm {
	Log log = LogFactory.getLog(UserRegForm.class);

	private static final long serialVersionUID = 1L;
	
	private UserVO user = new UserVO(new UserSettingsVO());
	
	//Used to verify email field value
	private String emailCheck;
	
	//Used to verify password field value
	private String passwordCheck;

	public UserVO getUser() {
		return user;
	}

	public void setUser(UserVO user) {
		this.user = user;
	}


	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = super.validate(mapping, request);
		
		if(user.getPassword() == null || user.getPassword().equals("") || passwordCheck == null || passwordCheck.equals("")){
			//errors.add("password", new ActionMessage("errors.password.empty"));
		} else if(!user.getPassword().equals(passwordCheck)){
			user.setPassword("");
			passwordCheck = "";
			errors.add("password", new ActionMessage("errors.password.check"));
		}
		if(user.getEmail() == null || user.getEmail().equals("") || emailCheck == null || emailCheck.equals("")){
			//errors.add("email", new ActionMessage("errors.email.empty"));
		} else if(!user.getEmail().equals(emailCheck)){
			errors.add("password", new ActionMessage("errors.email.check"));
		}
		/*if(user.getFirstName() == null || user.getFirstName().equals("")){
			errors.add("First Name", new ActionMessage("errors.firstName.empty"));
		}
		if(user.getLastName() == null || user.getLastName().equals("")){
			errors.add("Last Name", new ActionMessage("errors.lastName.empty"));
		}
		if(!errors.isEmpty()){
			log.warn("Errors Found In UserRegForm...Returning Errors...");
		}*/
		return errors;
	}

	public void setPasswordCheck(String passwordCheck) {
		this.passwordCheck = passwordCheck;
	}

	public String getPasswordCheck() {
		return passwordCheck;
	}

	/**
	 * @param emailCheck the emailCheck to set
	 */
	public void setEmailCheck(String emailCheck) {
		this.emailCheck = emailCheck;
	}

	/**
	 * @return the emailCheck
	 */
	public String getEmailCheck() {
		return emailCheck;
	}

	
/*	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		log.debug("In UserRegForm Reset() method...");
		this.user = new UserVO(new UserSettingsVO());
		this.emailCheck = null;
		this.passwordCheck = null;
	}
*/
	
}
